//
//  ZGSBookShelfViewController.m
//  MapleLeaf
//
//  Created by dllo on 15/11/26.
//  Copyright © 2015年 AntennaBabyTeam. All rights reserved.
//
#import "ZGSBookCaseCell.h"
#import "ZGSBookCaseColCell.h"
#import "ZGSBookShelfViewController.h"
#import "ZGSCartoonHistoryModel.h"
#import "ZGSHomeCartoonModel.h"
#import "ZGSCartoonDetailsViewController.h"
#import "ZGSTypeListViewController.h"
#import "ZGSReadModel.h"
#import "SaveTool.h"
typedef NS_ENUM(NSUInteger, ShowType) {
    tableType,
    collectionType,
};

@interface ZGSBookShelfViewController ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate, UIAlertViewDelegate>
@property (nonatomic, retain) UISegmentedControl *segmented;
@property (nonatomic, retain) UIScrollView *scrollView;

@property (nonatomic, retain) UICollectionViewFlowLayout *bookCaseFlowLayout;
@property (nonatomic, retain) UICollectionViewFlowLayout *historyFlowLayout;
@property (nonatomic, retain) UITableView *bookCaseTableView;
@property (nonatomic, retain) UICollectionView *bookCaseCollectionView;
@property (nonatomic, retain) UITableView *historyTableView;
@property (nonatomic, retain) UICollectionView *historyCollectionView;

@property (nonatomic, retain) NSMutableArray *bookCaseArr;
@property (nonatomic, retain) NSMutableArray *historyArr;

@property (nonatomic, assign) ShowType showType;

@property (nonatomic, retain) UILabel *noneLabel;
@property (nonatomic, retain) UILabel *secNoneLabel;

@property (nonatomic, retain) UIImageView *headImageView;


@end



@implementation ZGSBookShelfViewController
#pragma mark - DataMethod

- (void)getData {
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"BookCase/bookCase.plist"];
    NSMutableArray *bookCaseArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    self.bookCaseArr = bookCaseArr;
    
    NSString *cacheSandBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *cachePath = [cacheSandBoxPath stringByAppendingPathComponent:@"CartoonHistory/history.plist"];
    NSMutableArray *historyArr = [NSKeyedUnarchiver unarchiveObjectWithFile:cachePath];
    self.historyArr = historyArr;
    
    if (self.bookCaseArr.count < 1) {
        
        self.bookCaseTableView.hidden = YES;
        self.bookCaseCollectionView.hidden = YES;
        
        self.noneLabel.hidden = NO;
    }
    else {
        
        if (self.showType == tableType) {
            self.bookCaseTableView.hidden = NO;
        }
        else {
            self.bookCaseCollectionView.hidden = NO;
        }
        
        [self.bookCaseTableView reloadData];
        [self.bookCaseCollectionView reloadData];
        
        self.noneLabel.hidden = YES;
    }
    
    if (self.historyArr.count < 1) {
        
        self.secNoneLabel.hidden = NO;
        
        self.historyTableView.hidden = YES;
        self.historyCollectionView.hidden = YES;
        
        
    }
    else {
        
        if (self.showType == tableType) {
            self.historyTableView.hidden = NO;
        }
        else {
            self.historyCollectionView.hidden = NO;
        }
        
        [self.historyTableView reloadData];
        [self.historyCollectionView reloadData];
        
        self.secNoneLabel.hidden = YES;
    }
}



#pragma mark - ViewMethod

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.961f green:0.325f blue:0.173f alpha:1.00f];
    
    [self getData];
    [self createSeg];
    
    // segmented初始选择
    self.segmented.selectedSegmentIndex = self.scrollView.contentOffset.x / WIDTH;
    
    self.segmented.hidden = NO;
    
    // 是否显示logo
    if (self.isShowHeadImage) {
        self.headImageView.hidden = YES;
    }
    else {
        self.headImageView.hidden = NO;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-qiehuan(1) 14-31-25-725.png"] style:UIBarButtonItemStylePlain target:self action:@selector(changeShowType:)];
    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.segmented.selectedSegmentIndex = 0;
    self.showType = tableType;
    
    // 无收藏记录
    self.noneLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH + WIDTH / 2 - 70, HEIGHT / 2.0 - 50, 140, 20)];
    self.noneLabel.text = @"暂无收藏";
    self.noneLabel.textAlignment = NSTextAlignmentCenter;
    [self.scrollView addSubview:self.noneLabel];
    self.noneLabel.hidden = YES;
    
    self.secNoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 2 - 70, HEIGHT / 2 - 50, 140, 20)];
    self.secNoneLabel.text = @"暂无下载";
    self.secNoneLabel.textAlignment = NSTextAlignmentCenter;
    [self.scrollView addSubview:self.secNoneLabel];
    self.secNoneLabel.hidden = YES;
    
}

- (void)viewWillDisappear:(BOOL)animated {
    self.segmented.hidden = YES;
    self.headImageView.hidden = YES;
    [self.segmented removeFromSuperview];
    self.segmented = nil;
    
    [self.historyTableView reloadData];
    [self.bookCaseTableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark CreatViews

- (void)createSeg {
    if (!_segmented) {
        self.segmented = [[UISegmentedControl alloc] initWithItems:@[@"下载", @"收藏"]];
        self.segmented.frame = CGRectMake(WIDTH / 2 - 70, 25, 140, 28);
        self.segmented.layer.borderColor = [[UIColor whiteColor] CGColor];
        self.segmented.tintColor = [UIColor whiteColor];
        [self.segmented addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
        [self.navigationController.view addSubview:self.segmented];
        
        self.headImageView = [[UIImageView alloc] init];
        self.headImageView.frame = CGRectMake(10.0 * WIDTH / 375.0, 15, WIDTH / 4.0, 44);
        self.headImageView.image = [UIImage imageNamed:@"headImage.png"];
        [self.navigationController.view addSubview:self.headImageView];
    }
}



#pragma mark - lazyLoad

- (UIScrollView *)scrollView {
    
    if (!_scrollView) {
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 - 49)];
        self.scrollView.contentSize = CGSizeMake(WIDTH * 2, 0);
        self.scrollView.pagingEnabled = YES;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        [self.view addSubview:self.scrollView];
    }
    return _scrollView;
}

- (UITableView *)historyTableView {
    
    if (!_historyTableView) {
        self.historyTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 - 49)];
        self.historyTableView.rowHeight = 110 / 667.0 * HEIGHT;
        self.historyTableView.separatorStyle = NO;
        [self.scrollView addSubview:self.historyTableView];
        self.historyTableView.dataSource = self;
        self.historyTableView.delegate = self;
        
        [self.historyTableView registerClass:[ZGSBookCaseCell class] forCellReuseIdentifier:@"bcReuse"];
    }
    return _historyTableView;
}

- (UICollectionViewFlowLayout *)historyFlowLayout {
    
    if (!_historyFlowLayout) {
        self.historyFlowLayout = [[UICollectionViewFlowLayout alloc] init];
        self.historyFlowLayout.itemSize = CGSizeMake((WIDTH - 43) / 3, 171 * HEIGHT / 667);
        self.historyFlowLayout.minimumInteritemSpacing = 10;
        self.historyFlowLayout.minimumLineSpacing = 10;
        self.historyFlowLayout.sectionInset = UIEdgeInsetsMake(11, 11, 11, 11);
    }
    return _historyFlowLayout;
}

- (UICollectionView *)historyCollectionView {
    
    if (!_historyCollectionView) {
        self.historyCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 - 49) collectionViewLayout:self.historyFlowLayout];
        [self.scrollView addSubview:self.historyCollectionView];
        self.historyCollectionView.backgroundColor = [UIColor whiteColor];
        self.historyCollectionView.hidden = YES;
        
        [self.historyCollectionView registerClass:[ZGSBookCaseColCell class] forCellWithReuseIdentifier:@"ccReuse"];
        
        self.historyCollectionView.dataSource = self;
        self.historyCollectionView.delegate = self;
        
    }
    return _historyCollectionView;
}

- (UITableView *)bookCaseTableView {
    
    if (!_bookCaseTableView) {
        self.bookCaseTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH, 0, WIDTH, HEIGHT - 64 - 49)];
        self.bookCaseTableView.rowHeight = 110.0 * HEIGHT / 667.0;
        self.bookCaseTableView.separatorStyle = NO;
        [self.scrollView addSubview:self.bookCaseTableView];
        self.bookCaseTableView.dataSource = self;
        self.bookCaseTableView.delegate = self;
        
        [self.bookCaseTableView registerClass:[ZGSBookCaseCell class] forCellReuseIdentifier:@"bbcReuse"];
    }
    return _bookCaseTableView;
}

- (UICollectionViewFlowLayout *)bookCaseFlowLayout {
    
    if (!_bookCaseFlowLayout) {
        self.bookCaseFlowLayout = [[UICollectionViewFlowLayout alloc] init];
        self.bookCaseFlowLayout.itemSize = CGSizeMake((WIDTH - 43) / 3, 171 * HEIGHT / 667);
        self.bookCaseFlowLayout.minimumInteritemSpacing = 10;
        self.bookCaseFlowLayout.minimumLineSpacing = 10;
        self.bookCaseFlowLayout.sectionInset = UIEdgeInsetsMake(11, 11, 11, 11);
    }
    return _bookCaseFlowLayout;
}

- (UICollectionView *)bookCaseCollectionView {
    
    if (!_bookCaseCollectionView) {
        self.bookCaseCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(WIDTH, 0, WIDTH, HEIGHT - 64 - 49) collectionViewLayout:self.bookCaseFlowLayout];
        [self.scrollView addSubview:self.bookCaseCollectionView];
        self.bookCaseCollectionView.backgroundColor = [UIColor whiteColor];
        self.bookCaseCollectionView.hidden = YES;
        
        [self.bookCaseCollectionView registerClass:[ZGSBookCaseColCell class] forCellWithReuseIdentifier:@"bccReuse"];
        
        self.bookCaseCollectionView.dataSource = self;
        self.bookCaseCollectionView.delegate = self;
        
    }
    return _bookCaseCollectionView;
}



#pragma mark - tableViewDateSource & Delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.historyTableView) {
        return self.historyArr.count;
    }
    else if (tableView == self.bookCaseTableView) {
        return self.bookCaseArr.count;
    }
    else {
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.historyTableView) {
        ZGSBookCaseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"bcReuse" forIndexPath:indexPath];
        ZGSCartoonHistoryModel *cartoonHistory = self.historyArr[self.historyArr.count - indexPath.row - 1];
        ZGSHomeCartoonModel *cartoon = cartoonHistory.cartoon;
        NSString * encodingString = [cartoon.coverPic stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [cell.picView sd_setImageWithURL:[NSURL URLWithString:encodingString] placeholderImage:nil];
        cell.titleLabel.text = cartoon.name;
        if (cartoonHistory.hSet == -1) {
            cell.lookNumber.text = [NSString stringWithFormat:@"看到%@", [cartoonHistory.listArr[0] name]];
        }
        else {
            cell.lookNumber.text = [NSString stringWithFormat:@"看到%@", [cartoonHistory.listArr[cartoonHistory.hSet] name]];
        }
        cell.sumNumber.text = [NSString stringWithFormat:@"更新到%@", [cartoonHistory.listArr.lastObject name]];
        
        cell.readButton.tag = 2000 + indexPath.row;
        [cell.readButton addTarget:self action:@selector(readAction:) forControlEvents:UIControlEventTouchUpInside];
        
        // 长按手势
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
        //     把手势添加到cell上
        [cell addGestureRecognizer:longPress];
        longPress.minimumPressDuration = 0.7;
        
        return cell;
    }
    else if (tableView == self.bookCaseTableView) {
        ZGSBookCaseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"bbcReuse" forIndexPath:indexPath];
        ZGSCartoonHistoryModel *cartoonHistory = self.bookCaseArr[indexPath.row];
        ZGSHomeCartoonModel *cartoon = cartoonHistory.cartoon;
        NSString * encodingString = [cartoon.coverPic stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [cell.picView sd_setImageWithURL:[NSURL URLWithString:encodingString] placeholderImage:nil];
        cell.titleLabel.text = cartoon.name;
        if (cartoonHistory.hSet == -1) {
            cell.lookNumber.text = [NSString stringWithFormat:@"看到%@", [cartoonHistory.listArr[0] name]];
        }
        else if (cartoonHistory.hSet > 0) {
            cell.lookNumber.text = [NSString stringWithFormat:@"看到%@", [cartoonHistory.listArr[cartoonHistory.hSet] name]];
        }
        else {
            cell.lookNumber.text = @"未阅读";
        }
        cell.sumNumber.text = [NSString stringWithFormat:@"更新到%@", [cartoonHistory.listArr.lastObject name]];
        
        if ([cell.lookNumber.text isEqualToString:@"未阅读"]) {
            [cell.readButton setTitle:@"阅读" forState:UIControlStateNormal];
        } else {
            [cell.readButton setTitle:@"续看" forState:UIControlStateNormal];
        }
        
        cell.readButton.tag = 3000 + indexPath.row;
        [cell.readButton addTarget:self action:@selector(readAction:) forControlEvents:UIControlEventTouchUpInside];
        
        return cell;
    }
    else {
        return nil;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.historyTableView) {
        ZGSCartoonDetailsViewController *cdVC = [[ZGSCartoonDetailsViewController alloc] init];
        cdVC.hidesBottomBarWhenPushed = YES;
        ZGSCartoonHistoryModel *cartoonHistory = self.historyArr[self.historyArr.count - indexPath.row - 1];
        ZGSHomeCartoonModel *cartoon = cartoonHistory.cartoon;
        cdVC.albumId = cartoon.albumId;
        cdVC.hSet = cartoonHistory.hSet;
        cdVC.hPage = cartoonHistory.hPage;
        [self.navigationController pushViewController:cdVC animated:YES];
        self.segmented.hidden = YES;
    }
    else if (tableView == self.bookCaseTableView) {
        ZGSCartoonDetailsViewController *cdVC = [[ZGSCartoonDetailsViewController alloc] init];
        cdVC.hidesBottomBarWhenPushed = YES;
        ZGSCartoonHistoryModel *cartoonHistory = self.bookCaseArr[indexPath.row];
        ZGSHomeCartoonModel *cartoon = cartoonHistory.cartoon;
        cdVC.albumId = cartoon.albumId;
        cdVC.hSet = cartoonHistory.hSet;
        cdVC.hPage = cartoonHistory.hPage;
        [self.navigationController pushViewController:cdVC animated:YES];
        self.segmented.hidden = YES;
    }
}



#pragma mark - collectionViewDateSource & Delegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (collectionView == self.historyCollectionView) {
        return self.historyArr.count;
    }
    else if (collectionView == self.bookCaseCollectionView) {
        return self.bookCaseArr.count + 1;
    }
    else {
        return 0;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.historyCollectionView) {
        ZGSBookCaseColCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ccReuse" forIndexPath:indexPath];
        ZGSCartoonHistoryModel *cartoonHistory = self.historyArr[self.historyArr.count - indexPath.row - 1];
        ZGSHomeCartoonModel *cartoon = cartoonHistory.cartoon;
        NSString * encodingString = [cartoon.coverPic stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [cell.picView sd_setImageWithURL:[NSURL URLWithString:encodingString] placeholderImage:nil];
        cell.picView.frame = CGRectMake(0, 0, (WIDTH - 42) / 3, 135 * HEIGHT / 667);
        cell.titleLabel.text = cartoon.name;
        cell.sumNumber.text = [cartoonHistory.listArr.lastObject name];
        return cell;
    }
    else if (collectionView == self.bookCaseCollectionView) {
        ZGSBookCaseColCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"bccReuse" forIndexPath:indexPath];
        if (indexPath.row == self.bookCaseArr.count) {
            cell.picView.frame = CGRectMake(0, 0, (WIDTH - 42) / 3, 170 * HEIGHT / 667);
            cell.picView.image = [UIImage imageNamed:@"add.png"];
            cell.titleLabel.text = @"";
            cell.sumNumber.text = @"";
            return cell;
        }
        else {
            ZGSCartoonHistoryModel *cartoonHistory = self.bookCaseArr[indexPath.row];
            ZGSHomeCartoonModel *cartoon = cartoonHistory.cartoon;
            NSString * encodingString = [cartoon.coverPic stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [cell.picView sd_setImageWithURL:[NSURL URLWithString:encodingString] placeholderImage:nil];
            cell.picView.frame = CGRectMake(0, 0, (WIDTH - 42) / 3, 135 * HEIGHT / 667);
            cell.titleLabel.text = cartoon.name;
            cell.sumNumber.text = [cartoonHistory.listArr.lastObject name];
        }
        return cell;
    }
    else {
        return nil;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.historyCollectionView) {
        ZGSCartoonDetailsViewController *cdVC = [[ZGSCartoonDetailsViewController alloc] init];
        cdVC.hidesBottomBarWhenPushed = YES;
        ZGSCartoonHistoryModel *cartoonHistory = self.historyArr[self.historyArr.count - indexPath.row - 1];
        ZGSHomeCartoonModel *cartoon = cartoonHistory.cartoon;
        cdVC.albumId = cartoon.albumId;
        cdVC.hSet = cartoonHistory.hSet;
        cdVC.hPage = cartoonHistory.hPage;
        [self.navigationController pushViewController:cdVC animated:YES];
        self.segmented.hidden = YES;
    }
    else if (collectionView == self.bookCaseCollectionView) {
        if (indexPath.row == self.bookCaseArr.count) {
            ZGSTypeListViewController *tlVC = [[ZGSTypeListViewController alloc] init];
            tlVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:tlVC animated:YES];
        }
        else {
            ZGSCartoonDetailsViewController *cdVC = [[ZGSCartoonDetailsViewController alloc] init];
            cdVC.hidesBottomBarWhenPushed = YES;
            ZGSCartoonHistoryModel *cartoonHistory = self.bookCaseArr[indexPath.row];
            ZGSHomeCartoonModel *cartoon = cartoonHistory.cartoon;
            cdVC.albumId = cartoon.albumId;
            cdVC.hSet = cartoonHistory.hSet;
            cdVC.hPage = cartoonHistory.hPage;
            [self.navigationController pushViewController:cdVC animated:YES];
            self.segmented.hidden = YES;
        }
    }
}



#pragma mark - ButtonMethod
// 改变现实样式
- (void)changeShowType:(UIBarButtonItem *)button {
    if (self.showType == collectionType) {
        if (self.historyArr.count > 0) {
            self.historyTableView.hidden = NO;
            self.historyTableView.userInteractionEnabled = YES;
            self.historyCollectionView.hidden = YES;
            self.historyCollectionView.userInteractionEnabled = NO;
        }
        
        if (self.bookCaseArr.count > 0) {
            self.bookCaseTableView.hidden = NO;
            self.bookCaseTableView.userInteractionEnabled = YES;
            self.bookCaseCollectionView.hidden = YES;
            self.bookCaseCollectionView.userInteractionEnabled = NO;
        }
        
        self.showType = tableType;
    }
    else if (self.showType == tableType) {
        if (self.historyArr.count > 0) {
            self.historyTableView.hidden = YES;
            self.historyTableView.userInteractionEnabled = NO;
            self.historyCollectionView.hidden = NO;
            self.historyCollectionView.userInteractionEnabled = YES;
        }
        
        if (self.bookCaseArr.count > 0) {
            self.bookCaseTableView.hidden = YES;
            self.bookCaseTableView.userInteractionEnabled = NO;
            self.bookCaseCollectionView.hidden = NO;
            self.bookCaseCollectionView.userInteractionEnabled = YES;
        }
        
        self.showType = collectionType;
    }
}

// 改变显示种类
- (void)segAction:(UISegmentedControl *)seg {
    if (seg.selectedSegmentIndex == 0) {
        self.scrollView.contentOffset = CGPointMake(0, 0);
    }
    else if (seg.selectedSegmentIndex == 1) {
        self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
    }
}

// 续读功能
- (void)readAction:(UIButton *)button {
#if 0
    if (button.tag < 3000) {
        ZGSCartoonHistoryModel *cartoonHistory = self.historyArr[self.historyArr.count - (button.tag - 2000) - 1];
        if (cartoonHistory.hSet == -1) {
            ZGSReadModel *read = cartoonHistory.listArr[0];
            if ([read.unLock integerValue] == 0) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"抱歉~" message:@"该章节为收费章节" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
                [alert show];
            }
            else {
                ZBReadCartoonViewController *rcVC = [[ZBReadCartoonViewController alloc] init];
                rcVC.hidesBottomBarWhenPushed = YES;
                rcVC.cartoon = cartoonHistory.cartoon;
                rcVC.listArr = [NSMutableArray array];
                rcVC.listArr = cartoonHistory.listArr;
                rcVC.hSet = cartoonHistory.hSet;
                rcVC.hPage = cartoonHistory.hPage;
                [self.navigationController pushViewController:rcVC animated:YES];
                [rcVC release];
            }
        }
        else {
            ZBReadModel *read = cartoonHistory.listArr[cartoonHistory.hSet];
            if ([read.unLock integerValue] == 0) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"抱歉~" message:@"该章节为收费章节" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
                [alert show];
            }
            else {
                ZBReadCartoonViewController *rcVC = [[ZBReadCartoonViewController alloc] init];
                rcVC.hidesBottomBarWhenPushed = YES;
                rcVC.cartoon = cartoonHistory.cartoon;
                rcVC.listArr = [NSMutableArray array];
                rcVC.listArr = cartoonHistory.listArr;
                rcVC.hSet = cartoonHistory.hSet;
                rcVC.hPage = cartoonHistory.hPage;
                [self.navigationController pushViewController:rcVC animated:YES];
                [rcVC release];
            }
        }
        
    }
    else {
        ZBCartoonHistoryModel *cartoonHistory = self.bookCaseArr[(button.tag - 3000)];
        if (cartoonHistory.hSet == -1) {
            ZBReadModel *read = cartoonHistory.listArr[0];
            if ([read.unLock integerValue] == 0) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"抱歉~" message:@"该章节为收费章节" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
                [alert show];
            }
            else {
                ZBReadCartoonViewController *rcVC = [[ZBReadCartoonViewController alloc] init];
                rcVC.hidesBottomBarWhenPushed = YES;
                rcVC.cartoon = cartoonHistory.cartoon;
                rcVC.listArr = [NSMutableArray array];
                rcVC.listArr = cartoonHistory.listArr;
                rcVC.hSet = cartoonHistory.hSet;
                rcVC.hPage = cartoonHistory.hPage;
                [self.navigationController pushViewController:rcVC animated:YES];
                [rcVC release];
            }
        }
        else {
            ZBReadModel *read = cartoonHistory.listArr[cartoonHistory.hSet];
            if ([read.unLock integerValue] == 0) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"抱歉~" message:@"该章节为收费章节" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
                [alert show];
            }
            else {
                ZBReadCartoonViewController *rcVC = [[ZBReadCartoonViewController alloc] init];
                rcVC.hidesBottomBarWhenPushed = YES;
                rcVC.cartoon = cartoonHistory.cartoon;
                rcVC.listArr = [NSMutableArray array];
                rcVC.listArr = cartoonHistory.listArr;
                rcVC.hSet = cartoonHistory.hSet;
                rcVC.hPage = cartoonHistory.hPage;
                [self.navigationController pushViewController:rcVC animated:YES];
                [rcVC release];
            }
        }
        
    }
#endif
}

// 长按删除
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress {
    if (longPress.state == UIGestureRecognizerStateBegan) {
        ZGSBookCaseCell *cell = (ZGSBookCaseCell *)longPress.view;
        if (cell.readButton.tag < 3000) {
            ZGSCartoonHistoryModel *cartoonHistory = self.historyArr[self.historyArr.count - (cell.readButton.tag - 2000) - 1];
            ZGSHomeCartoonModel *cartoon = cartoonHistory.cartoon;
            
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:[NSString stringWithFormat:@"确定把\"%@\"从历史中删除吗", cartoon.name] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
            alertView.tag = cell.readButton.tag + 2000;
            [alertView show];
        }
    }
}



#pragma mark - alertView Delegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        if (alertView.tag < 5000) {
            ZGSCartoonHistoryModel *cartoonHistory = self.historyArr[self.historyArr.count - (alertView.tag - 4000) - 1];
            [SaveTool cancleFileInPlist:cartoonHistory type:SaveToolCartoonHistory];
            [self.historyArr removeObject:cartoonHistory];
            [self.historyTableView reloadData];
        }
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
